var WxParse = require('../../wxParse/wxParse.js');
var app = getApp();
let openid;
let nickName;
let avatarUrl;
let jumpurl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: true,
    hasget:true,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    yd_content:"",
    point:[{
      nums:'0',
      balance:'0'
    }],
    Orders:[{
      alled:'1',
      waited:'2',
      geted:'0',
      served:'4'
    }],
    show:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
   var that=this;
   jumpurl=app.jumpurl.url;
   that.setData({
     hasget: app.globalData.userInfo
   })
   
    // 查看是否授权
    wx.getSetting({
      success: function (res) {
        
        console.log(res);
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              
              app.globalData.userInfo = res.userInfo;
              
            }
          })
          
        // 传头像图片和昵称给后台
          wx.request({
            url: jumpurl + 'my/index',
            data:{
              openid:app.userList.openid,
              shopid:app.userList.shopid,
              nickname: app.globalData.userInfo.nickName,
              avatar: app.globalData.userInfo.avatarUrl,
            },
            success:function(res){
              console.log(app.userList.openid);
              console.log(app.userList.shopid)
              console.log(res);
              var youdou = res.data.youdou;
              var money = res.data.money;
              if(!youdou){youdou=0};
              if(!money){money=0}
              that.setData({
                youdou:youdou,
                quanbu:res.data.quanbu,
                money:money
                // daisong:res.data.daisong,
                // daiqu:res.data.daiqu,
                // daifu:res.data.daifu,
              })
            },
            fail:function(err){
                console.log("**************错误信息*****************");
                console.log(err)
            }
          })
        }
      }, fail: function () {
        console.log("未获取用户信息")
      }
    })
    // 获取用户头像信息
    // if (app.globalData.userInfo) {
    //   this.setData({
    //     userInfo: app.globalData.userInfo,
    //     hasUserInfo: true
    //   })
    // } else if (this.data.canIUse) {
    //   // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
    //   // 所以此处加入 callback 以防止这种情况
    //   app.userInfoReadyCallback = res => {
    //     this.setData({
    //       userInfo: res.userInfo,
    //       hasUserInfo: true
    //     })
    //   }
    // } 
    // else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      // wx.getUserInfo({
      //   success: res => {
      //     app.globalData.userInfo = res.userInfo
      //     this.setData({
      //       userInfo: res.userInfo,
      //       hasUserInfo: true
      //     })
      //   }
      // })
    },

  
    // 头像信息获取结束

  // },
  // getUserInfo: function (e) {
  //   console.log(e)
  //   app.globalData.userInfo = e.detail.userInfo
  //   this.setData({
  //     userInfo: e.detail.userInfo,
  //     hasUserInfo: true
  //   })
  // },

  // getUserInfoBtn:function(res){
  //   console.log("**********获取用户信息***********");
  //   wx.getUserInfo({
  //     success: res => {
      
  //       console.log(res);
  //       app.globalData.userInfo = res.userInfo
  //       this.setData({
  //         userInfo: res.userInfo,
  //         hasUserInfo: true
  //       })
  //     }
  //   })
  // },

  bindGetUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo;
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true  ,
     
      hasget: app.globalData.userInfo
        })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */

  onShow: function () {
    var that=this;

    /***用getCurrentPages() 获取页面堆栈，再根据 index 获取想要操作的页面 */

    
    var pages = getCurrentPages(); //获取页面栈
    var currpage = pages[pages.length - 1]; //当前页面
    var prevPage = pages[pages.length - 2]; //上一个页面（父页面）
    if (prevPage){
      var prevPageroute=prevPage.route;
      var index_route = getCurrentPages()[0].route;
      var mine_route = "pages/mine/mine";
      console.log("---------current******************");
      console.log(pages.length);

     
      if (pages.length ==3 && prevPageroute==currpage.route){
        wx.navigateBack({
          delta:1
        })
      }
    }else{
      
    }
   
    // 传头像图片和昵称给后台,获取

    wx.request({
      url: jumpurl + 'my/index',
      data: {
        openid: app.userList.openid,
        shopid: app.userList.shopid,
        nickname: app.globalData.userInfo.nickName,
        avatar: app.globalData.userInfo.avatarUrl,
      },
      success: function (res) {
        that.setData({
          youdou: res.data.youdou,
          quanbu: res.data.quanbu,
          money: res.data.money,
          daisong: res.data.daisong,
          daiqu: res.data.daiqu,
          daifu: res.data.daifu,
        })
      },
      fail: function (err) {
        console.log("**************错误信息*****************");
        console.log(err)
      }
    })

    
  },
  // 余额跳转
  balance:function(){
    wx.navigateTo({
      url: '/pages/yue/yue',
    })
  },
  // 全部订单跳转
  fullOrder:function(){
    this.data.navigateToNextDelta = true
    var tips = 0;
    console.log(tips)
    wx.navigateTo({
      url: '/pages/orders/orders?tips=' + tips
    })
    // wx.redirectTo({
    //   url: '/pages/orders/orders?tips=' + tips
    // })
  },
  // 待支付订单跳转
  Payment:function(){
    var tips = 1;
    this.data.navigateToNextDelta = true
    wx.navigateTo({
      url: "/pages/orders/orders?tips=" + tips
    })
  },
  // 待取件跳转
  Waitget: function () {
    var tips = 2;
    this.data.navigateToNextDelta = true
    wx.navigateTo({
      url: "/pages/orders/orders?tips=" + tips
    })
  },
  // 待送达跳转
  Service: function () {
    var tips = 3;
    this.data.navigateToNextDelta = true
    wx.navigateTo({
      url: "/pages/orders/orders?tips=" + tips
    })
  },

  // 设置中心跳转
  SetCenter: function () {
    wx.navigateTo({
      url: '/pages/setting/setting',
    })
  },
   //20180816常用地址跳转 
  cyAddress:function(){
    wx.navigateTo({
      url: '/pages/addresslist/addresslist',
    })
  },
  // 申请快递员
  Courier: function () {
    wx.navigateTo({
      url: '/pages/identy/identy',
    })
  },
  // 加盟代理
  Franchise: function () {
    wx.navigateTo({
      url: '/pages/joinment/joinment',
    })
  },
  //跳转常见问题
  questions:function(){
    wx.navigateTo({
      url: '/pages/question/question',
    })
  },
//点击优豆余额
  ydgz:function(){
    var that=this;
    wx.request({
      url:jumpurl+ 'my/youdou',
      data:{
        shopid:app.userList.shopid
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded" //post
      },
      success: function (res) {
        console.log(res)
        var article = res.data.content;
        WxParse.wxParse('article', 'html', article, that, 5);
        that.setData({
          show: true
        })
      },
      fail: function (err) {
        console.log("调用onload失败" + err)
      }
    })
   

  },
  //点击我知道了
  iknow:function(){
    var that = this;
    that.setData({
      show:false
    })
  },
 




})